<?php


namespace Part19_CombinationMode\Code1;

/**
 * 枝节点，用来存储子部件，在component接口中实现与子部件有关的操作
 */
class Composite extends Component
{
    // 定义一个子对象集合来存储其下属的枝节点和叶节点
    private $children;

    public function add($c)
    {
        $this->children[] = $c;
    }

    public function remove($c)
    {
        $key = array_search($c, $this->children, true);
        if ($key) {
            unset($this->children[$key]);
        } else {
            echo '移除元素失败' . "\n";
        }
    }

    public function display($depth)
    {
        echo str_repeat('-', $depth) . $this->name . "\n";

        foreach ($this->children as $child) {
            $child->display($depth + 2);
        }
    }
}